<html>
  <head>
    <title>Test media source playback with different containers.</title>
  </head>
  <body onload="runTest();">
    <video></video>
    <script src='eme_player_js/app_loader.js' type='text/javascript'></script>
    <script type="text/javascript">

      // The time in secs to play the streams. Several encrypted streams have
      // 1/2 second clear leads, so play at least a second to ensure that the
      // encrypted content is handled.
      var PLAY_TIME_SEC = 1.0;

      var video = document.querySelector('video');

      function onTimeUpdate() {
        Utils.timeLog('timeupdate @ ' + video.currentTime);
        if (video.currentTime < PLAY_TIME_SEC)
          return;

        video.removeEventListener('timeupdate', onTimeUpdate);
        video.removeEventListener('ended', Utils.failTest);
        Utils.installTitleEventHandler(video, 'ended');

        // Seek to end to save test execution time. Most of the test videos
        // (e.g. the bear videos) are about 2.5 seconds long.
        video.currentTime = 3000;
      }

      function runTest() {
        video.addEventListener('timeupdate', onTimeUpdate);
        video.addEventListener('ended', Utils.failTest);

        var testConfig = new TestConfig();
        testConfig.loadQueryParams();

        var media_types = [];
        var media_files = [];
        if (testConfig.videoFormat) {
          media_types = media_types.concat(testConfig.videoFormat);
          media_files = media_files.concat(testConfig.videoFile);
        }
        if (testConfig.audioFormat) {
          media_types = media_types.concat(testConfig.audioFormat);
          media_files = media_files.concat(testConfig.audioFile);
        }

        var mediaSource =
            MediaSourceUtils.loadMediaSource(media_files, media_types);

        if (testConfig.runEncrypted > 0) {
          var emePlayer = PlayerUtils.createPlayer(video, testConfig);
          emePlayer.registerEventListeners()
              .then(function(player) {
                video.src = window.URL.createObjectURL(mediaSource);
              })
              .catch(function(error) {
                Utils.failTest('Unable to register EME event listeners.');
              });
        } else {
          video.src = window.URL.createObjectURL(mediaSource);
        }
        video.play();
      }
      </script>
  </body>
</html>
